﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UserManager.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="UserManager" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Business
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using Hisaab.Business.Entities;
    using Hisaab.Business.Interfaces;
    using Hisaab.Common;
    using Hisaab.Data.Interfaces;
    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="UserManager"/> type.
    /// </summary>
    public class UserManager : IUserManager
    {
        /// <summary>
        /// User repository
        /// </summary>
        private readonly IUserRepository _userRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="UserManager"/> class.
        /// </summary>
        public UserManager()
            : this(ServiceLocator.Current.GetInstance<IUserRepository>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UserManager"/> class.
        /// </summary>
        /// <param name="userRepository">The resolve.</param>
        public UserManager(IUserRepository userRepository)
        {
            Contract.Requires<ArgumentNullException>(userRepository != null);
            _userRepository = userRepository;
        }

        /// <summary>
        /// Gets the filtered.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="pageNo">The page no.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>List of filtered items</returns>
        public IEnumerable<User> List(User filter, int? pageNo, int? pageSize)
        {
            return _userRepository.Query(filter.Map<User, Data.Entities.User>(), pageNo, pageSize)
                .ToList()
                .Select(x => x.Map<Data.Entities.User, User>());
        }

        /// <summary>
        /// Adds the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns>Id of User added</returns>
        public int Add(User entity)
        {
            return _userRepository.Add(entity.Map<User, Data.Entities.User>());
        }

        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Update(User entity)
        {
            _userRepository.Update(entity.Map<User, Data.Entities.User>());
        }

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Delete(User entity)
        {
            _userRepository.Delete(entity.Map<User, Data.Entities.User>());
        }

        /// <summary>
        /// Validates the useer.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns>
        /// Id of user if authentication successful, -1 if authentication fails
        /// </returns>
        public int ValidateUser(string username, string password)
        {
            return _userRepository.ValidateUser(username, password);
        }

        /// <summary>
        /// Changes the password.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="oldPassword">The old password.</param>
        /// <param name="newPassword">The new password.</param>
        /// <param name="confirmPassword">The confirm password.</param>
        /// <returns>
        /// True if the password is changed successfully, false otherwise
        /// </returns>
        public bool ChangePassword(int userId, string oldPassword, string newPassword, string confirmPassword)
        {
            if (string.IsNullOrWhiteSpace(oldPassword) || string.IsNullOrWhiteSpace(newPassword) || !newPassword.Equals(confirmPassword, StringComparison.InvariantCulture))
            {
                return false;
            }

            var user = _userRepository.FindById(userId);
            if (user.Password.Equals(oldPassword))
            {
                user.Password = newPassword;
                _userRepository.Update(user);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Gets the secret question.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns>Secret question of user</returns>
        public string GetSecretQuestion(int userId)
        {
            var user = _userRepository.FindById(userId);
            if (user != null)
            {
                return user.SecretQuestion;
            }

            return string.Empty;
        }

        /// <summary>
        /// Updates the secret question.
        /// </summary>
        /// <param name="userId">The logged user.</param>
        /// <param name="secretQuestion">The secret question.</param>
        /// <param name="secretAnswer">The secret answer.</param>
        /// <returns>
        /// True if the update is successfully, false otherwise
        /// </returns>
        public bool UpdateSecretQuestion(int userId, string secretQuestion, string secretAnswer)
        {
            var user = _userRepository.FindById(userId);
            if (user != null)
            {
                user.SecretQuestion = secretQuestion;
                user.SecretAnswer = secretAnswer;
                _userRepository.Update(user);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Gets the currency symbol.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns>Locale of currency for user</returns>
        public string GetCurrencySymbol(int userId)
        {
            var user = _userRepository.FindById(userId);
            if (user != null)
            {
                return user.CurrencyLocale ?? string.Empty;
            }

            return string.Empty;
        }

        /// <summary>
        /// Sets the currency symbol.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="symbol">The locale.</param>
        /// <returns>True if the update is successfully, false otherwise</returns>
        public bool SetCurrencySymbol(int userId, string symbol)
        {
            var user = _userRepository.FindById(userId);
            if (user != null)
            {
                user.CurrencyLocale = symbol;
                _userRepository.Update(user);
                return true;
            }

            return false;
        }
    }
}